// pages/xin-address/address.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";

var array = require('./city.js');
var array1 = []
for (var x in array.postList) {
  array1.push(array.postList[x].name)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switch1Checked:'',
    array: array1,
    currentcityindex: 0,
    item: "",
    items:'',
    id:'',
    selectbable: '',
    name: "",
    tel: "",
    detailadd: "",
    lng:'',
    lat:'',
    //省市区
    province:'',
    city:'',
    region:'',
    // 省市区id
    provinceID:'',
    cityID:'',
    regionID:'',
    // 消费者id
    personID:'',
    // 消费者姓名
    personName:'',
    sitelist:'',
    xuanzes:''
  },
  switch1Change: function () {
    this.setData({ switch1Checked: !this.data.switch1Checked })
  },
  xuanze: function () {
    var currentcity = this.data.array[this.data.currentcityindex]
    var bianji='编辑'
    wx.navigateTo({
      url: '/pages/address-position/add-posi?currentcity=' + currentcity + '&bianji=' + bianji + '&currentcityindex=' + this.data.currentcityindex ,
    })
  },
  selectnow: function (e) {
    var index = e.currentTarget.dataset.index;
      this.setData({
        selectbable: index,
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    if (options.xuanze){
      this.setData({
        xuanzes: options.xuanze
      })

      wx.setStorageSync("xuanzes", options.xuanze)
    }
    if (options.str){
      var list = JSON.parse(options.str)
      this.setData({ 
        sitelist:  list,
        id: list.ID,
        name: list.ConsigneeName,
        tel: list.phone,
        detailadd: list.Detailedaddress,

        // currentcityindex: list.currentcityindex,

        items: list.Locationaddress,
        lng: list.Longitude,
        lat: list.latitude,
        //省市区
        province: list.Province,
        city: list.City,
        region: list.Region,
        // 省市区id
        provinceID: list.ProvinceID,
        cityID: list.CityID,
        regionID: list.RegionID,
        // 消费者id
        personID: list.PersonID,
        // 消费者姓名
        personName: list.ConsigneeName,
        switch1Checked: list.Defaultaddress,
      })
      wx.setStorageSync("sitelist", this.data.sitelist)
    }

    if(this.data.city){
      var aa = this.data.city
      aa=aa.substring(0, aa.length - 1)
      console.log(aa)

      for(var x in this.data.array){
        if (aa == this.data.array[x]){
          this.setData({
            currentcityindex:x
           })
        }
      }
      
    }

    if (this.data.switch1Checked == 0){
      this.setData({
        switch1Checked : true
      })
    }else{
      this.setData({
        switch1Checked: false
      })
    }
    this.setData({ selectbable: this.data.sitelist.Label})
    if (options.item) {
      var item = JSON.parse(options.item)
      this.setData({ item: item })
      this.setData({ currentcityindex: item.currentcityindex })
    }else{
      // this.setData({ item: this.data.sitelist.Locationaddress })
    }
    var value = wx.getStorageSync('sitelist')
    if (value) {
      this.setData({
        sitelist:wx.getStorageSync('sitelist')  
      })
      var list = this.data.sitelist
      this.setData({
        sitelist: list,
        id: list.ID,
        name: list.ConsigneeName,
        tel: list.phone,
        detailadd: list.Detailedaddress,
        // currentcityindex: list.City
        items: list.Locationaddress,
        lng: list.Longitude,
        lat: list.latitude,
        //省市区
        province: list.Province,
        city: list.City,
        region: list.Region,
        // 省市区id
        provinceID: list.ProvinceID,
        cityID: list.CityID,
        regionID: list.RegionID,
        // 消费者id
        personID: list.PersonID,
        // 消费者姓名
        personName: list.ConsigneeName,
        // switch1Change: list.Defaultaddress,
      })
    }else{
      wx.setStorageSync("sitelist", this.data.sitelist) 
    }
  },
  // 姓名
  getname: function (e) {
    console.log(e)
    var name = e.detail.value
    this.setData({
      name: name
    })
  },
  // 手机号
  gettel: function (e) {
    var tel = e.detail.value
    this.setData({
      tel: tel
    })
  },
  // 详细地址
  getdetailadd: function (e) {
    var detailadd = e.detail.value
    this.setData({
      detailadd: detailadd
    })
  },
  //选择的城市
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      currentcityindex: e.detail.value,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  isPhoneNumber: function (tel) {
    var reg = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
    return reg.test(tel);
  },

  baocun: function () {
    if (this.data.name == "") {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.item == "" && this.data.items == "") {
      wx.showToast({
        title: '请选择地址',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.isPhoneNumber(this.data.tel)) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.detailadd == "") {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var queue = []
    console.log(111111111111)
    var obj = {
      ID: this.data.id,
      ConsigneeName: this.data.name,
      Phone: this.data.tel,
      // 省市区
      ProvinceName: this.data.item.province ? this.data.item.province : this.data.province,
      CityName: this.data.item.city ? this.data.item.city : this.data.city,
      DistrictName: this.data.item.district ? this.data.item.district : this.data.region,
      // 定位地址
      Locationaddress: this.data.item.address ? this.data.item.address : this.data.items,
      // 详细地址
      Detailedaddress: this.data.detailadd ? this.data.detailadd : this.data.detailadd,

      Label: this.data.selectbable,
      // Number
      // 标签 0家 1公司 2学校
      // 经纬度
      Longitude: this.data.item.location ? this.data.item.location.lng : this.data.lng,
      latitude: this.data.item.location ? this.data.item.location.lat : this.data.lat,
      // 是否默认
      Defaultaddress: this.data.switch1Checked ? 0 : 1,
    }

    console.log(obj,'obj')
    queue.push(obj)

    ajax('/api/MyaddressUpdate/ress?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      queue: queue,

    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "post",
      }, res => {
        console.log(res)
        if (res.Message = "上传成功!") {
          wx.showToast({
            title: '上传成功',
            icon: 'none',
            duration: 2000
          })
          if (wx.getStorageSync("xuanzes") == '选择'){
            wx.redirectTo({
              url: '/pages/selectaddress/selectaddress',
            })
          } else if (wx.getStorageSync("xuanzes") == '管理'){
            wx.redirectTo({
              url: '/pages/adminaddress/adminaddress',
            })
          }
          
        }
          // wx.navigateBack({
          //   delta: 1
          // })
      }
    )
    wx.removeStorageSync('sitelist')
  },
  // 删除
  shanchu:function(){
    ajax('/api/MyaddressDelete/ress?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      ID: this.data.id,

    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "post",
      }, res => {
        console.log(res)
        if (res.Message = "上传成功!") {
          wx.showToast({
            title: '删除成功',
            icon: 'none',
            duration: 2000
          })
          if (wx.getStorageSync("xuanzes") == '选择') {
            wx.redirectTo({
              url: '/pages/selectaddress/selectaddress',
            })
          } else if (wx.getStorageSync("xuanzes") == '管理') {
            wx.redirectTo({
              url: '/pages/adminaddress/adminaddress',
            })
          }
        }
      }
    )
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})